package amushen.two.html.impl;

import java.util.Iterator;
import java.util.LinkedHashMap;

import amushen.two.html.BaseComponent;
import amushen.two.html.BaseContainer;

public class Combo extends BaseContainer {
	
	private LinkedHashMap children=new LinkedHashMap();
	private String defaultValue="";
	public String getTagName() {		
		return "SELECT";
	}

	public Combo() {
	}
	
	public Combo(String name,LinkedHashMap children,String defaultValue){
		this();
		setId(name);
		this.defaultValue=defaultValue;
		setChildren(children);		
	}
	
	public void setChildren(LinkedHashMap children){		
		this.children=children;
		// add children option
		this.clear();
		if(children==null)return;
		Iterator it=children.keySet().iterator();
		while(it.hasNext()){
			String key=(String)it.next();
			String value=(String)children.get(key);
			Option option=new Option(key,value,false);
			if(this.defaultValue.equals(value)){
				option.setDefault(true);
			}
			addChild(option);
		}
	}
	public LinkedHashMap getChildren(){
		return this.children;		
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	
	
	
	private static class Option extends BaseComponent{

		public String getTagName() {
			return "OPTION";
		}
		public Option() {
		}
		public Option(String text,String value,boolean isDefault){
			getProperties().put("value",value);
			setContent(text);
			if(isDefault){
				getProperties().put("selected","selected");
			}
		}
		public void setDefault(boolean isDefault){
			if(isDefault)
				getProperties().put("selected","selected");
			else
				getProperties().put("selected",null);
		}
	}
}
